package com.firmboy.sql;

import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DBHelper {
	
	private static Connection conn = null;
	
	public static List<Map> queryForList(String sql,Connection conn,Object[] params){
		List<Map> list = new ArrayList<Map>();
		return list;
	}
	
	
	public static List<Map<String,Object>> returnSetToList(ResultSet rs) throws Exception{
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		ResultSetMetaData metaData = rs.getMetaData();
		int count = metaData.getColumnCount();
		Object value = null;
		while (rs.next()) {//循环遍历返回的结果集的每一行
			resultMap = new LinkedHashMap<String,Object>();
			for(int i = 1; i<=count; i++){
				int columnType = metaData.getColumnType(i);
				String columnName = metaData.getColumnName(i);
				if(columnType==Types.CLOB){//判断是否是长字符类型
					Clob clob = rs.getClob(i);
					if (clob != null)
						value = clob.getSubString(1, (int) clob.length());
					else
						value = null;
				} else
					value = rs.getObject(i);
				resultMap.put(columnName, value);
			}
			result.add(resultMap);
		}
		return result;
	}
	
}
